import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: code.rookie
 * @ClassName: SubdomainVisitCount
 * @Date: 2021/10/12 4:13 下午
 * @Version: 1
 * @Description 子域名访问计数
 */
public class SubdomainVisitCount {


    public static void main(String[] args) {
        String[] cpdomains = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String cpdomain : cpdomains) {
            String[] str1 = cpdomain.split("\\.|\\s+");
            int times = Integer.parseInt(str1[0]);
            StringBuilder sb = new StringBuilder();
            for (int i = str1.length - 1; i > 0; --i) {
                sb.insert(0, str1[i]);
                map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) + times);
                sb.insert(0, '.');
            }
        }

        List<String> results = new ArrayList<String>();
        for (String domain : map.keySet()) {
            results.add(map.get(domain) + " " + domain);
        }
    }
}
